<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table='blog_category';
    protected $primaryKey='cate_id';
    public $timestamps = false;
    protected $guarded=[];

    public function tree(){
        $cate = $this->orderBy('cate_order','asc')->get();
        $data = $this->getTree($cate);
        return $data;
    }
    public function getTree($data){
        $arr = array();
        foreach($data as $k=>$v){
            if($v->cate_pid==0){
                $arr[]= $data[$k];
                foreach($data as $m=>$n){
                    if($v->cate_id==$n->cate_pid){
                        $data[$m]['cate_name'] = '------'. $data[$m]['cate_name'];
                        $arr[]= $data[$m];
                    }
                }
            }
        }
        return $arr;
    }
    public function getPidCate(){
        $data = $this->where('cate_pid','=',0)->get();
        return $data;
    }
    public function getCate($id){
        $data = $this->where('cate_id','=',$id)->first();
       return $data;
    }
}
